package com.niit.galgame.controller;

import com.niit.entity.Game;
import com.niit.entity.Vedio;
import com.niit.galgame.service.impl.GameServiceImpl;
import com.niit.utils.CommonResult;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.*;
import java.util.List;

@RestController
@RequestMapping("game")
public class GameController {
    @Resource
    private GameServiceImpl gameService;

    @PostMapping("upload")
    public CommonResult uploadGame(Game game,
                                   @RequestParam("op") MultipartFile opFile,
                                   @RequestParam("ed") MultipartFile edFile,
                                   @RequestParam("imgs[]") MultipartFile[] imgsFiles){
        try {
            int gid = gameService.insertGame(game);
            gameService.upLoadFile(opFile.getInputStream(), "op.webm", gid);
            Vedio vedio1 = new Vedio();
            vedio1.setGid(gid);
            vedio1.setName("op");
            gameService.insertVedio(vedio1);

            gameService.upLoadFile(edFile.getInputStream(), "ed.webm", gid);
            Vedio vedio2 = new Vedio();
            vedio2.setGid(gid);
            vedio2.setName("ed");
            gameService.insertVedio(vedio2);
            int num = 1;
            for (MultipartFile imgFile : imgsFiles) {
                gameService.upLoadFile(imgFile.getInputStream(), num++ + ".png", gid);
            }
            return new CommonResult(200, "上传成功");
        } catch (Exception e) {
            System.out.println(e);
            return new CommonResult(500, "上传失败");
        }
    }
    @GetMapping("resource")
    public CommonResult getFile(HttpServletResponse response, @PathParam("gid")int gid, @PathParam("fileName") String fileName) {
            try{
                OutputStream outputStream = response.getOutputStream();
                InputStream inputStream = gameService.getFileStream(fileName, gid);
                IOUtils.copy(inputStream,outputStream);
                inputStream.close();
                outputStream.close();
                return new CommonResult(200,"获取成功");
            }catch (Exception e){
                return new CommonResult(500, "获取失败");
            }
    }



    @GetMapping("show")
    public CommonResult<Game> getGame(@PathParam("gid") int gid){
        Game game = gameService.queryById(gid);
        System.out.println(game);
        return new CommonResult<Game>(200,"查询成功",game);
    }

    @GetMapping("delete")
    public CommonResult deleteGame(@PathParam("gid") int gid){
        try {
            gameService.deleteGame(gid);
            return new CommonResult(200, "删除成功");
        }catch (Exception e){
            return new CommonResult(400, "删除失败");
        }
    }

    @GetMapping("/all")
    public CommonResult<List<Game>> findAll(){
        return new CommonResult<List<Game>>(200,"查找成功",gameService.findAll());
    }
}
